//
//  YFTrainingAlertPickerView.m
//  SquashSpark
//
//  Created by Coollang on 2017/11/17.
//  Copyright © 2017年 Coollang-YF. All rights reserved.
//

#import "YFTrainingAlertPickerView.h"
#import "YFUserSeriesViewModel.h"


@interface YFTrainingAlertPickerView()<UIPickerViewDelegate,UIPickerViewDataSource>
@property (weak, nonatomic) IBOutlet UIPickerView *pickerView;
@property (weak, nonatomic) IBOutlet UILabel *titlelabel;
@property (weak, nonatomic) IBOutlet UIButton *confirmButton;

/** 第一列默认显示的文字 */
@property (nonatomic, copy)NSString *firstColValue;
/** 第二列默认显示的文字 */
@property (nonatomic, copy)NSString *secondColValue;

/** Training目标训练时间  key:有Day week month*/
//@property (nonatomic, strong,readonly) NSDictionary <NSString *,NSArray *> *trainingHours;

/** trainHour */
@property (nonatomic, strong) NSArray *selectTrainhoursArr;

/** dayWeekMonth:时间区间 */
@property (nonatomic, strong) NSArray *timeSectionArr;

@property (nonatomic, copy)NSString *selectTimeSectionStr;
@property (nonatomic, copy)NSString *selectHoursStr;


@end

@implementation YFTrainingAlertPickerView

- (void)awakeFromNib {
    [super awakeFromNib];
    self.pickerView.delegate = self;
    self.pickerView.dataSource = self;
    [self clipsCornerRadius:6 borderWidth:1.5 borderColor:kYellowGreenColor];
    [self.confirmButton clipsCornerRadius:3];
}

- (IBAction)confirmButtonClick:(id)sender {
    if (self.didSelectHeight) {
        if (self.selectHoursStr.length <= 0) {
            self.selectHoursStr = self.selectTrainhoursArr[[self.pickerView selectedRowInComponent:0]];
        }
        if (self.selectTimeSectionStr.length <= 0) {
            self.selectTimeSectionStr = [self.timeSectionArr objectAtIndex:[self.pickerView selectedRowInComponent:1]];
        } self.didSelectHeight(self.selectHoursStr,self.selectTimeSectionStr);
    }
}

- (NSInteger)numberOfComponentsInPickerView:(nonnull UIPickerView *)pickerView {
    return 2;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
    if (component == 0) {
        return self.selectTrainhoursArr.count;
    }else {
        return self.timeSectionArr.count;
    }
}

- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view {
    ((UIView *)[pickerView.subviews objectAtIndex:1]).backgroundColor = [UIColor clearColor];
    ((UIView *)[pickerView.subviews objectAtIndex:2]).backgroundColor = [UIColor clearColor];
    UILabel *pickerLabel = (UILabel *)view;
    if (!pickerLabel) {
        pickerLabel = [[UILabel alloc] init];
        UIFont *font = kOpenSansFont(OpenSansExtraBold, 20);
        pickerLabel.font = font;
        pickerLabel.textColor = kTWhiteText;
    }
    NSString *title = @"";
    if (component == 0) {
        title = self.selectTrainhoursArr[row];
    }else{
        title = self.timeSectionArr[row];
    }
    pickerLabel.text = title;
    [pickerLabel sizeToFit];
    return pickerLabel;
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
    if (component == 0) {
        if (self.selectTrainhoursArr.count > row) {
            self.selectHoursStr = self.selectTrainhoursArr[row];
        }
    }else {
        self.selectTimeSectionStr = self.timeSectionArr[row];
    }
}

- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component {
    return 48;
}

// 设置初始值
- (void)configuerFirstColValue:(NSString *)firstColValue secondColValue:(NSString *)secondColValue {
    self.firstColValue = firstColValue;
    self.secondColValue = secondColValue;
    self.selectHoursStr = firstColValue;
    self.selectTimeSectionStr = secondColValue;
    
    if ([self.selectTrainhoursArr containsObject:self.firstColValue]) {
        NSInteger oneIndex = [self.selectTrainhoursArr indexOfObject:self.firstColValue];
        [self.pickerView selectRow:oneIndex inComponent:0 animated:NO];
    }
    if (self.timeSectionArr.count > 0 &&  [self.timeSectionArr containsObject:self.secondColValue]) {
        NSInteger twoIndex = [self.timeSectionArr indexOfObject:self.secondColValue];
        [self.pickerView selectRow:twoIndex inComponent:1 animated:NO];
    }
}

+ (instancetype)trainingAlertPickerView {
    YFTrainingAlertPickerView *view = [[NSBundle mainBundle] loadNibNamed:@"YFTrainingAlertPickerView" owner:nil options:nil].lastObject;
    return view;
}

- (NSArray *)timeSectionArr {
    if (_timeSectionArr == nil) {
        _timeSectionArr = @[@"per Day",@"per Week",@"per month"];
    }
    return _timeSectionArr;
}
- (NSArray *)selectTrainhoursArr {
    if (_selectTrainhoursArr == nil) {
        NSMutableArray *hoursM = [NSMutableArray array];
        for (int i = 1; i < 10; i++) {
            [hoursM addObject:[NSString stringWithFormat:@"%zdhour",i]];
        }
        _selectTrainhoursArr = hoursM.copy;
    }
    return _selectTrainhoursArr;
}



@end
